from math import prod


def production_cost_3(N, a11_18, a19, a21_28, a31_38, a41_43, a39, c1, c2, c3, c4, c5, c6, detect_parts, detect_halfproducts, detect_final, disassemble):
    """
    计算生产过程中的总成本。

    参数:
    N : int
        生产数量
    a11_18 : list
        零件1至8的次品率
    a19 : float
        成品次品率系数
    a21_28 : list
        零件1至8的单价
    a31_38 : list
        零件1至8的检测成本
    a41_43 : list
        半成品1至3的检测成本
    a39 : float
        成品检测成本
    c1 : float
        成品市场售价
    c2 : float
        不合格品调换损失
    c3 : float
        拆解费用
    c4 : float
        成品装配成本
    c5 : float
        回收收益系数
    c6 : float
        半成品装配成本
    detect_parts : list
        零件1至8的检测比例
    detect_halfproducts : list
        半成品1至3的检测比例
    detect_final : float
        成品检测比例
    disassemble : float
        不合格成品的拆解比例

    返回:
    total_cost : float
        总成本
    """

    # 零件1至8的采购和检测
    part_quality_rates = [1 - a * (1 - d) for a, d in zip(a11_18, detect_parts)]
    part_purchase_costs = [N / qr * a2 for qr, a2 in zip(part_quality_rates, a21_28)]
    part_testing_costs = [d * (N / (1 - a)) * a3 for a, d, a3 in zip(a11_18, detect_parts, a31_38)]

    # 半成品1、2、3的合格率
    quality_halfproduct1 = prod(part_quality_rates[0:3])
    quality_halfproduct2 = prod(part_quality_rates[3:6])
    quality_halfproduct3 = prod(part_quality_rates[6:8])

    # 半成品检测成本
    halfproduct_testing_costs = [d * N * a for d, a in zip(detect_halfproducts, a41_43)]

    # 成品的合格率
    product_quality_rate = quality_halfproduct1 * quality_halfproduct2 * quality_halfproduct3 * (1 - a19 * (1 - detect_final))

    # 半成品装配成本
    halfproduct_assembly_cost = N * c6

    # 成品装配成本
    assembly_cost = N * c4

    # 成品检测成本
    final_testing_cost = detect_final * N * a39

    # 不合格品的调换成本
    replacement_cost = (1 - detect_final) * N * (1 - product_quality_rate) * c2

    # 不合格成品的拆解成本和回收收益
    scrap_cost = disassemble * N * (1 - product_quality_rate) * c3
    scrap_revenue = disassemble * N * (1 - product_quality_rate) * 0.6 * c1

    # 总成本计算
    total_cost = sum(part_purchase_costs) + sum(part_testing_costs) + \
                 sum(halfproduct_testing_costs) + halfproduct_assembly_cost + \
                 assembly_cost + final_testing_cost + replacement_cost + \
                 scrap_cost - scrap_revenue

    import numpy as np

    # 假设已经定义了 production_cost_3 函数

    # 参数设置
    N = 1000  # 成品数量
    a11_18 = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]  # 零件1至8的次品率
    a19 = 0.1  # 成品次品率
    a21_28 = [2, 8, 12, 2, 8, 12, 8, 12]  # 零件1至8的购买单价
    a31_38 = [1, 1, 2, 1, 1, 2, 1, 2]  # 零件1至8的检测成本
    a39 = 6  # 成品的检测成本
    a41_43 = [4, 4, 4]  # 半成品的检测成本
    c1 = 200  # 成品市场售价
    c2 = 40  # 不合格产品调换损失
    c3 = 10  # 成品拆解费用
    c5 = 10  # 半成品拆解费用
    c4 = 8  # 成品装配成本
    c6 = 8  # 半成品装配成本

    n = 13  # 二进制数的位数
    num_combinations = 2 ** n  # 二进制数的总数

    # 生成13位数二进制矩阵,表示是否检测零件1-8、是否检测成品、是否拆解不合格成品
    binary_matrix = np.array(
        list(map(lambda x: np.array(list(map(int, bin(x)[2:].zfill(n)))), range(num_combinations))))

    minvalue = float('inf')
    minlabel = 0

    for i in range(num_combinations):
        # 决策变量：零件和半成品是否进行检测，成品是否检测，成品是否拆解
        detect_parts = binary_matrix[i, :8]  # 是否检测零件1至8
        detect_halfproducts = binary_matrix[i, 8:11]  # 是否检测半成品1、2、3
        detect_final = binary_matrix[i, 11]  # 是否检测成品
        disassemble = binary_matrix[i, 12]  # 是否拆解不合格成品

        # 调用生产成本计算函数
        total_cost = production_cost_3(N, a11_18, a19, a21_28, a31_38, a41_43, a39, c1, c2, c3, c4, c5, c6,
                                       detect_parts, detect_halfproducts, detect_final, disassemble)
        if total_cost < minvalue:
            minvalue = total_cost
            minlabel = i

    # 输出最小总成本的方案编号和决策变量
    print(f'Min case label: {minlabel}')
    print(binary_matrix[minlabel, :])
    print(f'Min total production cost: {minvalue}')
    return total_cost